import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Res } from '@nestjs/common';
import { OperlogService } from './operlog.service';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';
import { RepeatSubmit } from '../../../common/decorators/repeat-submit.decorator';
import { Response } from 'express';

@ApiTags('系统监控-操作日志')
@Controller('monitor/operlog')
export class OperlogController {
  constructor(private readonly operlogService: OperlogService) {}

  @ApiOperation({ summary: '操作日志-清除全部日志' })
  @RequirePermission('monitor:operlog:clean')
  @Delete('/clean')
  @Operlog({ businessType: BusinessType.CLEAN })
  removeAll() {
    return this.operlogService.removeAll();
  }

  @Get('/list')
  findAll(@Query() query: any) {
    return this.operlogService.findAll(query);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.operlogService.findOne(+id);
  }

  @ApiOperation({ summary: '操作日志-清除日志' })
  @RequirePermission('monitor:operlog:remove')
  @Operlog({ businessType: BusinessType.DELETE })
  @Delete(':id')
  @ApiParam({ name: 'id', required: true, description: '日志ID' })
  remove(@Param('id') ids: string) {
    const operlogIds = ids.split(',').map((id) => +id);
    return this.operlogService.remove(operlogIds);
  }

  @ApiOperation({ summary: '导出操作日志数据为xlsx文件' })
  @RequirePermission('monitor:operlog:export')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Res() res: any, @Body() body: any): Promise<void> {
    return this.operlogService.export(res, body);
  }
}
